//
//  GGuessVC.m
//  guess
//
//  Created by fank on 15/11/4.
//  Copyright © 2015年 fank. All rights reserved.
//

#import "GGuessVC.h"
#import "GHttpEnqine.h"
#import "MainGameViewController.h"
#import "Define.h"
#import "GuessDataModel.h"
#import "AudioSoundHelper.h"
#import <AVFoundation/AVAudioPlayer.h>
#import "QuestionModel.h"

@interface GGuessVC ()
@property (strong,nonatomic) NSString *str11;
@property (strong,nonatomic) NSMutableArray *array;
@end

static AVAudioPlayer *_audioPlayer = nil;

@implementation GGuessVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //背景颜色
    self.view.backgroundColor = UIColorFromRGBValue(0xf0ffff);
     _array = [NSMutableArray array];
    if (![ABTools getUserDefault:@"source"]) {
        [self getGuessinfoWitHContentName:@"chengyu.txt"];
    }
    [_startGameBtn addTarget:self action:@selector(startGameBtn:) forControlEvents:UIControlEventTouchUpInside];
    [_playMusicBtn addTarget:self action:@selector(openMusic:) forControlEvents:UIControlEventTouchUpInside];
    [_stopMusicBtn addTarget:self action:@selector(closeMusic:) forControlEvents:UIControlEventTouchUpInside];
    [_shareBtn addTarget:self action:@selector(shareBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    
    if (![ABTools getUserDefault:@"record"]) {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setObject:@100 forKey:@"gold"];
        [dict setObject:@1 forKey:@"number"];
        [ABTools setUserDefault:dict key:@"record"];
    }   
}

-(void)viewWillAppear:(BOOL)animated{
     [ABTools addAnimationWithView:_image2];
}

-(void)getGuessinfoWitHContentName:(NSString *)contentName
{
    //    通过指定的路径读取文本内容
    NSString *resourcePath = [ConciseKit resourcePath];
    NSString *filePath = [resourcePath stringByAppendingPathComponent:contentName];
    NSError *error=nil;
    NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
    NSArray *wordArray = [str componentsSeparatedByString:@"\r"];
    

    for (NSString *temp in wordArray) {
        
        if (![temp isEqualToString:@""]) {
            NSLog(@"%@",temp);
            NSError *error = nil;
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            [dict setObject:[temp componentsSeparatedByString:@"（"][0] forKey:@"title"];
            [dict setObject:[temp componentsSeparatedByString:@"） "][1] forKey:@"answer"];
            [dict setObject:[[temp componentsSeparatedByString:@"（"][1] componentsSeparatedByString:@"） "][0] forKey:@"prompt"];
            [_array addObject:dict];
        }
    }
    [ABTools setUserDefault:_array key:@"source"];
}

-(void) test3{
    NSString *str = _str11;
    //文件不存在会自动创建，文件夹不存在则不会自动创建会报错
    NSString *path = @"/Users/apple/Desktop/test_export.txt";
    NSError *error;
    [str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (error) {
        NSLog(@"导出失败:%@",error);
    }else{
        NSLog(@"导出成功");
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark-
#pragma mark-按钮点击事件
-(void)startGameBtn:(UIButton *)sender
{
    [AudioSoundHelper playSoundWithFileName:@"mainclick" ofType:@"mp3"];
    MainGameViewController *vc = [[MainGameViewController alloc]initWithNibName:@"MainGameViewController" bundle:nil];
    

    
    NSDictionary *dict = [ABTools getUserDefault:@"record"];
    if ([[dict objectForKey:@"number"] integerValue] > [[ABTools getUserDefault:@"source"] count]) {
        NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
        [dict1 setObject:@(1) forKey:@"number"];
        [dict1 setObject:@(100) forKey:@"gold"];
        [ABTools setUserDefault:dict1 key:@"record"];
    }

    [self presentViewController:vc animated:YES completion:nil];
}


- (void)openMusic:(id)sender
{
    [AudioSoundHelper playSoundWithFileName:@"mainclick" ofType:@"mp3"];
    
    [self musicPlay];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"AudioStatus"];
}

- (void)closeMusic:(id)sender;
{
    [AudioSoundHelper playSoundWithFileName:@"mainclick" ofType:@"mp3"];
    
    [self musicStop];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"AudioStatus"];
}

#pragma mark private

- (void)musicPlay
{
    _playMusicBtn.hidden = YES;
    _stopMusicBtn.hidden = NO;
    
    
    if (!_audioPlayer) {
        NSString *mfp = [[NSBundle mainBundle] pathForResource:@"bg0" ofType:@"mp3"];
        NSURL *url = [[NSURL alloc] initFileURLWithPath:mfp];
        _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    }
    
    if (![_audioPlayer isPlaying]) {
        [_audioPlayer prepareToPlay];
        [_audioPlayer setVolume:0.75];
        _audioPlayer.numberOfLoops = -1;
        [_audioPlayer play];
    }
}

- (void)musicStop
{
    _playMusicBtn.hidden = NO;
    _stopMusicBtn.hidden = YES;
    [_audioPlayer stop];
}

-(void)shareBtn:(id)sender
{
    [AudioSoundHelper playSoundWithFileName:@"mainclick" ofType:@"mp3"];
    [self shareMyAPP];
}

-(void)shareMyAPP
{
    NSLog(@"分享软件");
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
